<template>
  <el-dialog
    title="新增表单"
    :visible.sync="dialogVisible"
    width="30%"
    :append-to-body="true"
  >
    <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="表单名称" prop="name" label-width="100px">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="表单编码" prop="code" label-width="100px">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="所属模版" prop="modelId" label-width="100px">
        <el-select v-model="form.modelId" filterable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="表单模式" prop="runType" label-width="100px">
        <template slot-scope="scope">
          <el-switch
            v-model="form.runType"
            active-color="#13ce66"
            inactive-color="#C0CCDA"
          >
          </el-switch>
        </template>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('addForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import TreeSelect from "@/components/basic-treeselect/treeselect";
import { activeUrl } from "@/config/env";
const urls = {
  save: activeUrl + "/form/form/add",
  list: activeUrl + "/form/form/list",
  modul: activeUrl + "/modelList"
};
export default {
  data() {
    return {
      dialogVisible: false,
      data: [],
      defaultCheckedKeys: [],
      form: {
        name: null,
        code: null,
        runType: false,
        modelId: ""
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }]
      },
      formInline: {
        name: null,
        limit: 10,
        page: 1,
        total: 0,
        sorter: {}
      },
      options: [],
      result : []
    };
  },
  components: {
    TreeSelect
  },
  methods: {
    sortChange(column, prop, order) {
      this.formInline.sorter = {
        field: column.prop,
        order: column.order
      };
      this.fetch();
    },
    changePage(val) {
      this.formInline.page = val;
      this.fetch();
    },
    popoverHide(checkedIds, checkedData) {
      this.form.menus = checkedIds.toString();
    },
    show(modelId) {
      this.dialogVisible = true;
      if (this.$refs["addForm"]) {
        this.$refs["addForm"].resetFields();
      }
      // this.fetch();
      this.modelList(modelId);
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //表单模式
          if (this.form.runType) {
            this.form.runType = "html";
          } else {
            this.form.runType = "json";
          }
          this.result.map(item => {
            if(this.form.modelId == item.name) {
              this.form.modelId = item.id
              console.log(this.form.modelId)
            }
          })
          axios.post(urls.save, this.form).then(res => {
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
        }
        return false;
      });
    },
    fetch() {
      axios.post(urls.list, this.formInline).then(res => {
        this.data = res.data.result;
      });
    },
    modelList(modelId) {
      var params = {};
      params.category_id = modelId;
      axios.get(urls.modul, { params: { category_id: "" } }).then(res => {
        this.result = res.data.result;
        if(modelId) {
          var arr = []
          res.data.result.map((item,index) => {
            if(item.id === modelId) {
              this.form.modelId = item.name;
              arr.push(item);
            }
          })
          this.options = arr;
        }else {
          this.options = res.data.result;
        }
      });
    }
  }
};
</script>
<style scoped>
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
.el-select {
  width: 100%;
}
</style>
